package org.kl.auditsupervision.service.fnd;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.kl.bf.service.BasicService;
import org.kl.bf.utils.BasicConstants;
import org.kl.bf.utils.CRUDUtils;
import org.kl.bf.utils.KLDynamicSpecifications;
import org.kl.bf.utils.KLSearchFilter;
import org.kl.auditsupervision.entity.fnd.FndPost;
import org.kl.auditsupervision.repository.fnd.FndPostDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springside.modules.utils.Collections3;

import com.alibaba.fastjson.JSON;
/**
 * @author Code Generator
 * @since  2015-09-09 10:59:57
 * @see 岗位表  岗位表
 */
// Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional
public class FndPostService extends BasicService<FndPost> {
	@Autowired
	private FndPostDao postDao;
	
	public void save(FndPost entity, @RequestParam(value = "inserted") String detailInserted,
			@RequestParam(value = "updated") String detailUpdated, @RequestParam(value = "deleted") String detailDeleted) {
		postDao.save(entity);
		if (StringUtils.isNotBlank(detailInserted)) {
			List<FndPost> inserts = JSON.parseArray(detailInserted, FndPost.class);
			if (Collections3.isNotEmpty(inserts)) {
				for (FndPost a : inserts) {
					// 增加与主表相关的数据复制代码
					postDao.save(a);
				}
			}
		}
		if (StringUtils.isNotBlank(detailUpdated)) {
			List<FndPost> updates = JSON.parseArray(detailUpdated, FndPost.class);
			if (Collections3.isNotEmpty(updates)) {
				for (FndPost b : updates) {
					postDao.save(b);
				}
			}
		}
		if (StringUtils.isNotBlank(detailDeleted)) {
			List<FndPost> deletes = JSON.parseArray(detailDeleted, FndPost.class);
			if (Collections3.isNotEmpty(deletes)) {
				postDao.delete(deletes);
			}
		}
	}
	
	public List<FndPost> getAllPostanization() {
		return postDao.findAll(new Sort(Direction.ASC, "postNo"));
	}

	public FndPost getPost(String guid) {
		return postDao.find(FndPost.class,guid);
	}
	
	public FndPost getOnePost(String guid){
		return postDao.findOne(guid);
	}

	public void savePost(FndPost entity) {
		postDao.save(entity);
	}

	public void deletePost(String guid) {
		postDao.delete(guid);
	}

	public List<FndPost> getAllPost() {
		return postDao.findAll();
	}

	public List<FndPost> findByOrgIdAndEnable(String id)
	{
		return postDao.findByOrgIdAndEnable(id,BasicConstants.YES_INT);
		
	}
	public List<FndPost> findByOrgId(String id)
	{
		return postDao.findByOrgId(id);
		
	}
	public FndPost findByPostNoAndOrgId(String postNo,String id){
		return postDao.findByPostNoAndOrgId(postNo, id);
		
	}
	public FndPost findByPostNameAndOrgId(String postName,String id){
		return postDao.findByPostNameAndOrgId(postName, id);
		
	}
	
	public List<FndPost> findByHigherPostId(String id){
		return postDao.findByHigherPostId(id);
	}
	
	public Page<FndPost> getPostInfo(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType,
			String orderType) {
		PageRequest pageRequest = CRUDUtils.buildPageRequest(pageNumber, pageSize, sortType, orderType);
		Specification<FndPost> spec = buildSpecification(searchParams);
		Page<FndPost> postp = postDao.findAll(spec, pageRequest);
		Iterator<FndPost> iterator = postp.iterator();
		while(iterator.hasNext())
		{
			FndPost po = iterator.next();
			if(po.getHigherPostId() != null)
			{
				FndPost p = new FndPost();
				p = postDao.findOne(po.getHigherPostId());
				po.setHigherPost(p);
			}
		}
		return postp;
	}
	
	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<FndPost> buildSpecification(Map<String, Object> searchParams) {
		Map<String, KLSearchFilter> filters = KLSearchFilter.parse(searchParams);
		Specification<FndPost> spec = KLDynamicSpecifications.byKLSearchFilter(filters.values(), FndPost.class);
		return spec;
	}

	@Autowired
	public void setPostDao(FndPostDao postDao) {
		this.postDao = postDao;
	}

	
}
